<template>
  <div>
    <el-upload
        class="upload-demo"
        action=""
        :auto-upload="false"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :on-change="handleUpload"
        :file-list="fileList">
      <el-button size="medium" type="primary">{{main.clickToUpload}}</el-button>
    </el-upload>
  </div>
</template>

<script>
import {generateUUIDv4,Util_Edit_One_File} from "@/components/utils/useUtils";

export default {
  props: {
    // 正确写法：指定 type
    languageS: {
      type: String,  // 必须是构造函数
      default: 'chinese'    // 默认值可选
    },
    fileListParent: {
      type: Array,
      default: () => [] // 默认值应该是函数返回空数组
    }
  },
  data() {
    return {
      fileList: [
          {name: '',
            url: ''}],
      fileListSon:[
        {name:''}
      ],
      //切换语言，页面变量
      main:{
        clickToUpload:'点击上传',
      },
    };

  },
  methods: {
    changeLanguage(language){
      // 其他语言切换逻辑...
      this.componentKey = !this.componentKey;
      if (language==='chinese'){
        this.main.clickToUpload='点击上传'
      }
      else if (language==='english'){
        this.main.clickToUpload='Click to Upload'
      }
      else if (language==='russian'){
        this.main.clickToUpload='Нажмите для закачивать'
      }
    },
    //移除文件前提示
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    //移除文件
    handleRemove(file, fileList) {
      console.log("当前文件列表", fileList); // 这是Element UI维护的最新列表

      // 删除文件
      Util_Edit_One_File(3,'',null,file.name).then(data => {
        if (data === "操作成功") {
          // 更新fileListSon - 直接使用传入的fileList参数
          this.fileListSon = fileList.map(item => ({name: item.name}));

          // 同步更新this.fileList（如果需要）
          this.fileList = [...fileList];

          this.$emit('content-change', this.fileListSon);
        }
      });
    },
    //上传文件
    handleUpload(files, fileList) {
      //对新加入的文件修改文件名
      let fileName=generateUUIDv4()+'-'+files.name
      fileList[fileList.length-1].name=fileName
      files.name=fileName
      //向服务器传输文件
      Util_Edit_One_File(1,fileName,files.raw).then(data=>{
        if (data==="操作成功"){
          this.fileListSon.push({name:fileName})
          this.$emit('content-change', this.fileListSon)
        }
      })
    },

  },
  mounted() {
    this.fileList=this.fileListParent
    this.fileListSon=[]
    for (let i = 0; i < this.fileListParent.length; i++) {
      this.fileListSon.push(this.fileListParent[i].name)
    }

    // 初始化语言设置
    this.changeLanguage(this.languageS);
  }
}
</script>

<style scoped>

</style>